package com.zzptc.easyshop.dao;

import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.facory.DaoFacoryImp;
import com.zzptc.easyshop.vo.Good;
import com.zzptc.easyshop.vo.Product;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDao implements IDAO{
    @Override
    public boolean insert(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean delete(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean updata(VO vo) throws SQLException {
        return false;
    }

    @Override
    public List getAll() throws SQLException {
        MysqlDB db=new MysqlDB();
        Connection conn=null;
        conn=db.getConn();
        PreparedStatement ps=conn.prepareStatement("select * from easybuy_product");
        ResultSet rs=ps.executeQuery();
        List<Product> list=new ArrayList();
        while(rs.next()){
            Product product=new Product();
            product.setEp_id(rs.getInt(1));
            product.setEp_name(rs.getString(2));
            product.setEp_description(rs.getString(3));
            product.setEp_price(rs.getFloat(4));
            product.setEp_stock(rs.getInt(5));
            product.setEpc_id(rs.getInt(6));;
            product.setEpc_child_id(rs.getInt(7));
            product.setEp_file_name(rs.getString(8));
            list.add(product);
        }
        db.closeAll(conn,ps,rs);
        return list;
    }
    //根据商品id查询商品
    public Product getProductByid(int id)throws SQLException{
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        conn=db.getConn();
        ps=conn.prepareStatement("select * from easybuy_product where ep_id=?");
        ps.setInt(1,id);
        rs=ps.executeQuery();
//        List list=new ArrayList();
        Product product=null;
        if (rs.next()){
            product=new Product();
            product.setEp_id(rs.getInt(1));
            product.setEp_name(rs.getString(2));
            product.setEp_description(rs.getString(3));
            product.setEp_price(rs.getFloat(4));
            product.setEp_stock(rs.getInt(5));
            product.setEpc_id(rs.getInt(6));
            product.setEpc_child_id(rs.getInt(7));
            product.setEp_file_name(rs.getString(8));
//            list.add(product);
        }
        return product;
    }

    public static void main(String[] args) {
        ProductDao dao = new ProductDao();
        try {
            System.out.println(dao.getProductByid(1).getEp_name());
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        try {
//            dao=(ProductDao)DaoFacoryImp.getInstance().createDAO("productdao");
//            System.out.println(dao.getAll().size());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
